
import { addPhysicalTable, batchInsDataByTable } from '@/api/dynamicInterface';

import { getCurrentTime } from '@/utils/commonMethods';

const specialMixins = {
  data() {
    return {
    };
  },
  methods: {
    addPhysicalTable(obj, bodys) {
      const params = {
        tableId: this.specialHeaderDate[1].id,
        dataList: []
      };
      for (const key in obj) {
        params.dataList.push({
          col: key,
          symbol: 'eq',
          value: obj[key]
        });
      }
      params.dataList.push({
        col: 'Ssr',
        symbol: 'eq',
        value: localStorage.getItem('xcNickName')
      });
      params.dataList.push({
        col: 'Sssj',
        symbol: 'eq_d',
        value: getCurrentTime()
      });
      addPhysicalTable(params).then(res => {
        if (res.success) {
          const bodyParams = {
            tableId: this.specialHeaderDate[0].id,
            data: []
          };
          bodys.map(o => {
            const list = [];
            for (const key in o) {
              if (key !== 'S' && key !== 'id') {
                list.push({
                  col: key,
                  symbol: (o[key] + '').indexOf('-') !== -1 ? 'eq_d' : 'eq',
                  value: o[key] + ''
                });
              }
            }
            bodyParams.data.push(list);
          });

          console.log(bodyParams);
          batchInsDataByTable(bodyParams).then(res => {
            if (res.success) {
              this.$message.success('保存成功');
            } else {
              this.$message.warning('保存失败');
            }
          });
        }
      });
    },

    saveForm() {
      const bodys = this.$refs.commonComponents.formTable.bodys;
      const obj = this.$refs.commonComponents.formTable.searchObj;
      console.log(bodys);
      let count = 0;
      let objLnegth = 0;
      for (const key in obj) {
        if (obj[key] === '') {
          count++;
        }
        objLnegth++;
      }

      if (count === objLnegth) {
        this.$message.warning('搜索条件不能为空');
      } else {
        if (bodys.length === 0) {
          this.$message.warning('搜索结果为空,无法保存');
        } else {
          this.addPhysicalTable(obj, bodys);
        }
      }
    }
  }
};

export default specialMixins;

